package com.efrei.schoolmanager.views;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.efrei.schoolmanager.exceptions.LogicException;
import com.efrei.schoolmanager.tools.Tools;
import com.efrei.schoolmanager.views.components.CustomButton;
import com.efrei.schoolmanager.views.components.CustomFormattedTextField;
import com.efrei.schoolmanager.views.components.CustomTextField;

/**
 * JDialog simple permettant d'ajouter une absence (date de debut et de fin
 * ainsi que le raison)
 * 
 */
public class DialogAjouterAbsence extends JDialog {
	private static final long serialVersionUID = 5704828722406064033L;

	private final static String TITLE = "Absence";

	// Proprietaire de cette dialog(objectif: rendre modale la jdialog)
	private final JFrame proprio;

	// JPanels
	private JPanel titlePanel, formulairePanel, butonPanel;

	// JLabels
	private JLabel titleLabel, debutAbsenceLabel, finAbsenceLabel,
			raisonAbsenceLabel;

	// CustomTextField
	private CustomTextField raisonAbsenceTextField;

	// CustomFormattedTextField
	private CustomFormattedTextField debutAbsenceTextField,
			finAbsenceTextField;

	// CustomButton
	private CustomButton ajouterAbsenceButton;

	// Tableau de string contenant debut et fin de l'absence ainsi que
	// sa raison
	private String absences[];

	public DialogAjouterAbsence(JFrame frame) {
		super(frame, TITLE, true);
		proprio = frame;
		initLabels();
		initPanels();
		initTextFields();
		initButtons();
		remplirTitlePanel();
		remplirFormulairePanel();
		remplirButtonPanel();
		configurerVue();
	}

	/**
	 * Afficher la dialog et retourner le tableau de string a sa fermeture
	 * 
	 * @return
	 */
	public String[] showDialog() {
		setVisible(true);
		return absences;
	}

	private void remplirButtonPanel() {
		butonPanel = new JPanel(new FlowLayout());
		butonPanel.add(ajouterAbsenceButton);
	}

	private void remplirFormulairePanel() {
		formulairePanel = new JPanel();
		// Gestionnaire de positionnement
		GroupLayout layout = new GroupLayout(formulairePanel);
		formulairePanel.setLayout(layout);
		// Espacement des composants
		layout.setAutoCreateGaps(true);
		// Espacement entre composants et conteneur
		layout.setAutoCreateContainerGaps(true);

		// Groupe sequentiel sur l'axe horizontal
		GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();

		/* Constitution des groupes paralleles horizontalement */
		// 1e colonne
		hGroup.addGroup(layout.createParallelGroup()
				.addComponent(debutAbsenceLabel).addComponent(finAbsenceLabel)
				.addComponent(raisonAbsenceLabel));
		// 2e colonne
		hGroup.addGroup(layout.createParallelGroup()
				.addComponent(debutAbsenceTextField)
				.addComponent(finAbsenceTextField)
				.addComponent(raisonAbsenceTextField));
		layout.setHorizontalGroup(hGroup);

		/* Constitution des groupes paralleles verticalement */
		GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
		// 1e ligne
		vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
				.addComponent(debutAbsenceLabel)
				.addComponent(debutAbsenceTextField));
		// 2e ligne
		vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
				.addComponent(finAbsenceLabel)
				.addComponent(finAbsenceTextField));
		// 3e ligne
		vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
				.addComponent(raisonAbsenceLabel)
				.addComponent(raisonAbsenceTextField));
		layout.setVerticalGroup(vGroup);
	}

	private void remplirTitlePanel() {
		titlePanel.add(titleLabel);
	}

	/**
	 * Configurer les options de la vue
	 */
	private void configurerVue() {
		setLayout(new BorderLayout());
		this.add(titlePanel, BorderLayout.NORTH);
		this.add(formulairePanel, BorderLayout.CENTER);
		this.add(butonPanel, BorderLayout.SOUTH);
		Dimension preferedSize = getPreferredSize();
		Dimension dimensionIdeale = new Dimension(preferedSize.width,
				preferedSize.height + 28);
		setMinimumSize(dimensionIdeale);
		setSize(dimensionIdeale);
		this.setLocationRelativeTo(proprio);

	}

	private void initButtons() {
		ajouterAbsenceButton = new CustomButton("Ajouter l'absence");
		ajouterAbsenceButton
				.addActionListener(new ajouterAbsenceAtionListener());
	}

	private void initTextFields() {
		DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
		debutAbsenceTextField = new CustomFormattedTextField(format);
		finAbsenceTextField = new CustomFormattedTextField(format);
		raisonAbsenceTextField = new CustomTextField();
	}

	private void initPanels() {
		titlePanel = new JPanel(new FlowLayout());
		formulairePanel = new JPanel();
		butonPanel = new JPanel(new FlowLayout());
	}

	private void initLabels() {
		titleLabel = new JLabel(TITLE);
		debutAbsenceLabel = new JLabel("Debut d'absence :");
		finAbsenceLabel = new JLabel("Fin d'absence :");
		raisonAbsenceLabel = new JLabel("Raison :");
	}

	/**
	 * Listener : lorsque l'utilisateur valide l'absence, la fenetre se ferme,
	 * et renvoie à la classe appelante le tableau de string de l'absence
	 */
	class ajouterAbsenceAtionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			Date debutAbsence = null;
			Date finAbsence = null;
			try {
				debutAbsence = Tools.getDate(debutAbsenceTextField.getText(), "/");
				finAbsence = Tools.getDate(finAbsenceTextField.getText(), "/");
				//debut doit etre avnat la fin
				if (debutAbsence.after(finAbsence)) {
					throw new LogicException("Date de debut > date de fin");
				}
			} catch (LogicException e) {
				Tools.displayError(e.getMessage(), "Erreur date");
				return;
			}
			absences = new String[3];
			absences[0] = debutAbsenceTextField.getText();
			absences[1] = finAbsenceTextField.getText();
			absences[2] = raisonAbsenceTextField.getText();
			setVisible(false);
		}
	}
}
